/*
Copyright 2010 Sieric
http://sieric.ca

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package vanilla;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import savior.fs.FTP;
import savior.fs.FileSystem;
import savior.fs.Local;

public class Profile {

    private static FileSystem sourceOut = null;
    private static FileSystem destinationOut = null;
    private static List<String> ignoreListOut = null;

    public static boolean DeleteProfile(String _profileName) {
        return (new File(getBasePath() + _profileName)).delete();
    }

    public static String getBasePath() {
        String basePath = System.getProperty("user.home").replace('\\', '/') + "/";

        // Check if there is an Application Data directory (on Windows)
        if ((new File(basePath + "Application Data/")).exists()) {
            basePath += "Application Data/";
        }

        // Choose our directory
        basePath += "Savior/";

        // Create the directory if it doesn't exists
        File tmp = new File(basePath);
        if (!(tmp).exists()) {
            tmp.mkdir();
        }

        return tmp.getAbsolutePath().replace('\\', '/') + "/";
    }

    public static FileSystem getDestination() {
        return destinationOut;
    }

    public static List<String> getIgnoreList() {
        return ignoreListOut;
    }

    public static FileSystem getSource() {
        return sourceOut;
    }

    public static String[] ListProfiles() {
        return new File(getBasePath()).list();
    }

    public static String LoadProfile(String _profileName) {
        String result = "";

        try {
            FileReader pw = new FileReader(getBasePath() + _profileName);
            int len;

            char[] buffer = new char[1000];

            while ((len = pw.read(buffer)) > 0) {
                result += new String(buffer, 0, len);
            }

            pw.close();
        } catch (IOException e) {
            return null;
        }

        return result;
    }

    public static boolean ParseProfile(String _profileIn) {
        String[] words;

        // Initialize
        sourceOut = null;
        destinationOut = null;

        // Break in lines
        String[] lines = _profileIn.split("\n");
        if (lines.length < 2) {
            return false;
        }

        // Parse the source
        words = lines[0].split("\\|", -1);
        if ((words[0].compareTo("local") == 0) && (words.length == 2)) {
            sourceOut = new Local(words[1]);
        }
        if ((words[0].compareTo("ftp") == 0) && (words.length == 6)) {
            sourceOut = new FTP(words[1], words[2], words[3], words[4], words[5]);
        }

        // Parse the destination
        words = lines[1].split("\\|");
        if ((words[0].compareTo("local") == 0) && (words.length == 2)) {
            destinationOut = new Local(words[1]);
        }
        if ((words[0].compareTo("ftp") == 0) && (words.length == 6)) {
            destinationOut = new FTP(words[1], words[2], words[3], words[4], words[5]);
        }

        // Check if the two are found
        if ((sourceOut == null) || (destinationOut == null)) {
            return false;
        }

        // Parse the ignorelist
        ignoreListOut = new ArrayList<String>();
        for (int i = 2; i < lines.length; i++) {
            // Clean it
            lines[i].replaceAll("\r", "");
            if (lines[i].length() > 0) {
                ignoreListOut.add(lines[i]);
            }
        }


        return true;
    }

    public static boolean SaveProfile(String _profileName, String _profile) {
        try {
            PrintWriter pw = new PrintWriter(getBasePath() + _profileName);
            pw.print(_profile);
            pw.close();
        } catch (IOException e) {
            return false;
        }

        return true;
    }
}
